﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBS_semka_2
{
    class cRecord
    {
        private byte[] paKey;

        public byte[] aKey
        {
            get { return paKey; }
            set { paKey = value; }
        }

        private long piAddress;

        public long iAddress
        {
            get { return piAddress; }
            set { piAddress = value; }
        }

        private long piRowid;

        public long iRowid
        {
            get { return piRowid; }
            set { piRowid = value; }
        }

        public cRecord()
        {
            piAddress = -1;
            piRowid = -1;
        }

        public cRecord(byte[] pa_aKey, long pa_iRowid, long pa_iAddress)
        {
            paKey = pa_aKey;
            piRowid = pa_iRowid;
            piAddress = pa_iAddress;
        }

        public cRecord(int pa_iKeyLength, byte[] pa_iInput)
        {
            byte[] aBuffer = null;
            paKey = new byte[pa_iKeyLength];
            Array.Copy(pa_iInput, 0, paKey, 0, pa_iKeyLength);
           
            Array.Copy(pa_iInput, pa_iKeyLength, aBuffer, 0, 8);
            piRowid = BitConverter.ToInt64(aBuffer, 0);
            aBuffer = null;
            Array.Copy(pa_iInput, pa_iKeyLength + 8, aBuffer, 0, 8);
            piAddress = BitConverter.ToInt64(aBuffer, 0);
        }

        public byte[] toByteArray()
        {
            byte[] aReturn = new byte[paKey.Length + 16];
            Array.Copy(paKey, 0, aReturn, 0, paKey.Length);
            Array.Copy(System.BitConverter.GetBytes(iRowid), 0, aReturn, paKey.Length, 8);
            Array.Copy(System.BitConverter.GetBytes(iAddress), 0, aReturn, paKey.Length + 8, 8);
            return aReturn;
        }

        /// <summary>
        /// Porovna dva zaznamy podla klucov
        /// </summary>
        /// <param name="pa_aKey"></param>
        /// <returns></returns>
        public bool equalsKey(byte[] pa_aKey)
        {
            bool bReturn = true;
            if (paKey.Length == pa_aKey.Length)
            {
                for (int ii = 0; ii < paKey.Length; ii++)
                {
                    if (paKey[ii] != pa_aKey[ii])
                    {
                        bReturn = false;
                    }
                }
            }
            else
            {
                return false;
            }
            return bReturn;
        }

    }
}
